﻿/*
	WARNING!

	This script will drop all objects and data in the QIC schema.
	Please ensure that you have a valid backup before executing this script.

*/


SET NOCOUNT ON

PRINT 'CLEAN UP CODE'
BEGIN
	DECLARE @SQL nvarchar(max)
	SET @SQL = ''

	SELECT 
		@SQL += 'DROP ' + 
		CASE objects.type 
			WHEN 'P' THEN 'PROCEDURE'
			WHEN 'TF' THEN 'FUNCTION'
			WHEN 'IF' THEN 'FUNCTION'
			WHEN 'FN' THEN 'FUNCTION'
			WHEN 'U' THEN 'TABLE'
		END + ' ' +
		'QIC.' + QUOTENAME(objects.name) + CHAR(13) + CHAR(10)
	FROM sys.objects objects
	WHERE objects.schema_id = SCHEMA_ID('QIC')
	AND objects.type IN ('P','IF','TF','FN', 'U')

	EXEC (@SQL)
END

GO

PRINT 'CLEAN UP TYPES'
BEGIN
	DECLARE @SQL nvarchar(max)
	SET @SQL = ''

	SELECT @SQL += 'DROP TYPE ' + 'QIC.' + QUOTENAME(types.name) + CHAR(13) + CHAR(10)
	FROM sys.types types
	WHERE types.schema_id = SCHEMA_ID('QIC')

	EXEC (@SQL)
END
GO

PRINT 'DROP SCHEMA'
BEGIN
	IF SCHEMA_ID('QIC') IS NOT NULL
	BEGIN
		EXEC ('DROP SCHEMA [QIC]')
	END
END
GO

